package com.yike.order.controller;

import com.alibaba.fastjson.JSON;
import com.lkl.laop.sdk.LKLSDK;
import com.yike.common.Response.LklPayCallback;
import com.yike.common.base.BaseController;
import com.yike.common.enums.OperationLogType;
import com.yike.common.log.OperationLog;
import com.yike.common.request.SacsSeparateNotReq;
import com.yike.common.tool.StringUtil;
import com.yike.order.lkl.demo.BaseCommonDemo;
import com.yike.order.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;
@Slf4j
@RestController
@RequestMapping("/lkl/notice")
public class NoticeController extends BaseController {

    @Autowired
    private OrderService orderService;

    /**
     * 拉卡拉支付回调
     *
     * @return
     */
    @PostMapping("/lklCallback")
    @OperationLog(name = "拉卡拉支付回调", type = OperationLogType.ADD)
    public Map<String ,Object> lklCallback(HttpServletRequest request) throws Exception {
        //String body = "{\"payOrderNo\":\"21090611012001970631000463034\",\"merchan tOrderNo\":\"CH2021090613190866292\",\"orderInfo\":null,\"merchantNo\":\"822126090640003\",\"termId\":\"47781282\",\"tradeMerchantNo\":\"822126090640003\",\"tradeTermId\":\"47781282\",\"channelId\":\"10000038\",\"currency\":\"156\",\"amount\":1,\"tradeType\":\"PAY\",\"payStatus\":\"S\",\"notifyStatus\":0,\"orderCreateTime\":\"2021-09-06T05:19:43.000+00:00\",\"orderEfficientTime\":\"2021-09-06T05:19:43.000+00:00\",\"extendField\":null,\"payTime\":\"2021-09-06T05:19:43.000+00:00\",\"remark\":\"\",\"noticeNum\":1,\"sign\":null,\"notifyUrl\":null,\"notifyMode\":\"2\",\"payInfo\":\"1#1#ALIPAY#0#2021090622001432581427657317\",\"lklOrderNo\":\"2021090666210003610012\",\"crdFlg\":\"92\",\"payerId1\":\"2088702852632582\",\"payerId2\":\"rob***@126.com\",\"smCrdFlg\":\"01\",\"tradeTime\":\"20210906131943\",\"accountChannelOrderNo\":\"2021090622001432581427657317\",\"actualPayAmount\":1,\"logNo\":\"66210003610012\"}";
        //验签
//        String body =  this.getBody(request);
//
//        String authorization = request.getHeader("Authorization");

        String body = LKLSDK.notificationHandle(request);

        if (StringUtil.isEmpty(body)) {
            throw new Exception("验签失败");
        }
        LklPayCallback lklPayCallback = JSON.parseObject(body, LklPayCallback.class);
        return orderService.lklCallback(lklPayCallback);
    }



    @PostMapping("/lklCallbacks")
    @OperationLog(name = "拉卡拉支付回调", type = OperationLogType.ADD)
    public Map<String ,Object> lklCallbacks(HttpServletRequest request) throws Exception {
        //String body = "{\"payOrderNo\":\"21090611012001970631000463034\",\"merchan tOrderNo\":\"CH2021090613190866292\",\"orderInfo\":null,\"merchantNo\":\"822126090640003\",\"termId\":\"47781282\",\"tradeMerchantNo\":\"822126090640003\",\"tradeTermId\":\"47781282\",\"channelId\":\"10000038\",\"currency\":\"156\",\"amount\":1,\"tradeType\":\"PAY\",\"payStatus\":\"S\",\"notifyStatus\":0,\"orderCreateTime\":\"2021-09-06T05:19:43.000+00:00\",\"orderEfficientTime\":\"2021-09-06T05:19:43.000+00:00\",\"extendField\":null,\"payTime\":\"2021-09-06T05:19:43.000+00:00\",\"remark\":\"\",\"noticeNum\":1,\"sign\":null,\"notifyUrl\":null,\"notifyMode\":\"2\",\"payInfo\":\"1#1#ALIPAY#0#2021090622001432581427657317\",\"lklOrderNo\":\"2021090666210003610012\",\"crdFlg\":\"92\",\"payerId1\":\"2088702852632582\",\"payerId2\":\"rob***@126.com\",\"smCrdFlg\":\"01\",\"tradeTime\":\"20210906131943\",\"accountChannelOrderNo\":\"2021090622001432581427657317\",\"actualPayAmount\":1,\"logNo\":\"66210003610012\"}";
        //验签
//        String body =  this.getBody(request);
//
//        String authorization = request.getHeader("Authorization");

        String body = LKLSDK.notificationHandle(request);

        if (StringUtil.isEmpty(body)) {
            throw new Exception("验签失败");
        }
        LklPayCallback lklPayCallback = JSON.parseObject(body, LklPayCallback.class);
        return orderService.lklCallback(lklPayCallback);
    }


    @PostMapping("/sacsSeparateNot")
    @OperationLog(name = "拉卡拉分账回调", type = OperationLogType.ADD)
    public Map<String ,Object> sacsSeparateNot(HttpServletRequest request) throws Exception {
        //String body = "{\"payOrderNo\":\"21090611012001970631000463034\",\"merchan tOrderNo\":\"CH2021090613190866292\",\"orderInfo\":null,\"merchantNo\":\"822126090640003\",\"termId\":\"47781282\",\"tradeMerchantNo\":\"822126090640003\",\"tradeTermId\":\"47781282\",\"channelId\":\"10000038\",\"currency\":\"156\",\"amount\":1,\"tradeType\":\"PAY\",\"payStatus\":\"S\",\"notifyStatus\":0,\"orderCreateTime\":\"2021-09-06T05:19:43.000+00:00\",\"orderEfficientTime\":\"2021-09-06T05:19:43.000+00:00\",\"extendField\":null,\"payTime\":\"2021-09-06T05:19:43.000+00:00\",\"remark\":\"\",\"noticeNum\":1,\"sign\":null,\"notifyUrl\":null,\"notifyMode\":\"2\",\"payInfo\":\"1#1#ALIPAY#0#2021090622001432581427657317\",\"lklOrderNo\":\"2021090666210003610012\",\"crdFlg\":\"92\",\"payerId1\":\"2088702852632582\",\"payerId2\":\"rob***@126.com\",\"smCrdFlg\":\"01\",\"tradeTime\":\"20210906131943\",\"accountChannelOrderNo\":\"2021090622001432581427657317\",\"actualPayAmount\":1,\"logNo\":\"66210003610012\"}";
        //验签
        String body = LKLSDK.notificationHandle(request);

        if (StringUtil.isEmpty(body)) {
            throw new Exception("验签失败");
        }

        SacsSeparateNotReq req = JSON.parseObject(body, SacsSeparateNotReq.class);
        return orderService.sacsSeparateNot(req);
    }
}
